package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.home.activity.AuthDriverActivity
import com.stars.home.activity.DriverDetailsActivity
import com.stars.home.config.ConfigRx
import com.stars.home.inte.ChoiceData
import com.stars.home.model.CompreDetailsBean
import com.stars.home.model.User
import com.stars.home.utils.PopuTipsUtils
import com.stars.home.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.B
import com.trailer.base.utils.DialUtils
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

class GrabDetailsVModel(application: Application) : BViewModel<BRepository>(application)  {


    var order_id:String?=null
    var bean= MutableLiveData<CompreDetailsBean>()

    var position:String?="" //刷新首页数据用的


    var index:String?="" //刷新消息列表用的

    var ll:LinearLayout?=null


    fun isShowChat(type:Int):Int{
        if(type==1||type==2||type==3){
            return View.GONE
        }
        return View.VISIBLE
    }

    fun showTips(payment: Int):String{//0全款,1=到付,2=付定金
        if(payment==0){
            return " (全款) "
        }
        if(payment==1){
            return " (到付) "
        }
        return " (付定金) "
    }


    fun getInfo(){
        viewModelScope.launch {
            var it = repository().preq { NetEngine.service.orderDetail(order_id) }

            if (it.ok()) {
                bean.value=it.data

            }
        }
    }

    fun toCallPhone(mobile: String?) {
        //拨打电话
        if (null!=mobile&& TextUtils.isEmpty(mobile)) {
            toast("没有该货主的电话！！")
            return
        }
        DialUtils.dialPhone(view()?.context(), mobile!!)

    }





    fun toGradOrder(){

        //抢单

        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.grabbingOrders(bean.value?.order_id) }

            if(it.ok(true)){

                if(!TextUtils.isEmpty(position)){
                    RxBus.get().post(ConfigRx.REFRESH_DRIVER_HOME, position)
                }

                var driverOrderId=it.data?.order_id //司机的订单id

                UiSwitch.bundle(repository().context, DriverDetailsActivity::class.java,
                        B().putString("order_id", driverOrderId)
                                .ok())
                var con = repository().context as Activity
                con.finish()


            }

        }

    }



    fun isRead(id:String?){
        viewModelScope.launch {

            var it = repository().lreqTo { NetEngine.service.msgRead(id) }
            if (it.ok()) {
                RxBus.get().post(ConfigRx.REFRESH_MSG, index)
            }

        }


    }



}
